package com.benshou.bsoa.service.yida;

import com.aliyun.dingtalkyida_2_0.Client;
import com.aliyun.dingtalkyida_2_0.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.benshou.bsoa.dto.yida.req.YiDaReq;
import com.benshou.bsoa.utils.CreateAccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


/**
 * 宜搭相关表单实例创建
 * 获取的是表单实例详细数据 包括创建人
 * @auth hyh
 *  2025/04/28
 */
@Service
public class GetYiDaService {

    @Value("${yida_app_code}")
    private String APP_CODE;

    @Value("${yida_app_secret}")
    private String APP_SECRET;

    @Autowired
    private CreateAccessTokenUtil createAccessTokenUtil;


    /**
     * 查询表单实例数据
     * @param yiDaReq 对象
     * FormUuid 表单 UUID
     * UserId 用户 ID
     * @return 查询表单实例的响应信息 SearchFormDatasResponse
     * @throws Exception
     */
    public SearchFormDatasResponse queryListFormData(YiDaReq yiDaReq) throws Exception {
        // 创建 Client
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        Client client = new Client(config);

        // 准备 Headers
        SearchFormDatasHeaders headers = new SearchFormDatasHeaders();
        headers.xAcsDingtalkAccessToken = createAccessTokenUtil.getToken();

        // 准备 Request
        SearchFormDatasRequest request = new SearchFormDatasRequest()
                .setAppType(APP_CODE) // 宜搭类型中的应用类型
                .setSystemToken(APP_SECRET) // 宜搭类型中的应用密钥
                .setUserId(yiDaReq.getUserId()) // 用户 ID
                .setLanguage("zh_CN") // 语言
                .setFormUuid(yiDaReq.getFormUuid()) // 表单 UUID
                .setCurrentPage(yiDaReq.getCurrentPage()) // 当前页
                .setPageSize(yiDaReq.getPageSize()) // 每页条数
                .setUseAlias(true); // 是否使用别名

        try {
            // 调用查询接口
            SearchFormDatasResponse response = client.searchFormDatasWithOptions(request, headers, new RuntimeOptions());

            return response;
        } catch (TeaException err) {
            throw new RuntimeException("Error occurred while querying form data: " + err.getMessage(), err);
        } catch (Exception e) {
            TeaException err = new TeaException(e.getMessage(), e);
            throw new RuntimeException("Error occurred while querying form data: " + err.getMessage(), err);
        }
    }

    /**
     * 查询单条表单数据
     * @param yiDaReq
     * formInstanceId 表单实例 ID
     * FormUuid 表单 UUID
     * UserId 用户 ID
     * @return 查询表单实例的响应信息 GetFormDataByIDResponse
     * @throws Exception
     */
    public GetFormDataByIDResponse
    querySingleFormData(YiDaReq yiDaReq) throws Exception {
        // 创建 Client
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        Client client = new Client(config);

        // 准备 Headers
        GetFormDataByIDHeaders headers = new GetFormDataByIDHeaders();
        headers.xAcsDingtalkAccessToken = createAccessTokenUtil.getToken();

        // 准备 Request
        GetFormDataByIDRequest request = new GetFormDataByIDRequest()
                .setAppType(APP_CODE) // 宜搭类型中的应用类型
                .setSystemToken(APP_SECRET) // 宜搭类型中的应用密钥
                .setUserId(yiDaReq.getUserId()) // 用户 ID
                .setLanguage("zh_CN") // 语言
                .setFormUuid(yiDaReq.getFormUuid()) // 表单 UUID
                .setUseAlias(true); // 是否使用别名

        try {
            // 调用查询接口
            GetFormDataByIDResponse response = client.getFormDataByIDWithOptions(yiDaReq.getFormInstanceId(), request, headers, new RuntimeOptions());

            return response;
        } catch (TeaException err) {
            throw new RuntimeException("Error occurred while querying form data: " + err.getMessage(), err);
        } catch (Exception e) {
            TeaException err = new TeaException(e.getMessage(), e);
            throw new RuntimeException("Error occurred while querying form data: " + err.getMessage(), err);
        }
    }
}
